package com.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.common.JdbcUtils;

public class DaoUtils
{
    public static boolean add(String sqlName, Object params)
    {
        SqlSession session = JdbcUtils.getSession();

        if (session != null)
        {
            int result = session.insert(sqlName, params);
            session.commit();
            session.close();
            return result == 0 ? false : true;
        }
        return false;
    }

    public static boolean delete(String sqlName, Object params)
    {
        SqlSession session = JdbcUtils.getSession();
        if (session != null)
        {
            int result = session.delete(sqlName, params);
            session.commit();
            session.close();
            return result == 0 ? false : true;

        }
        return false;
    }

    public static List findList(String sqlName, Object params)
    {
        SqlSession session = JdbcUtils.getSession();

        if (session != null)
        {
            List result = null;
            try
            {
                result = session.selectList(sqlName, params);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            session.close();
            return result;
        }
        return null;
    }

    public static List findList(String sqlName)
    {
        SqlSession session = JdbcUtils.getSession();

        if (session != null)
        {
            List result = null;
            result = session.selectList(sqlName);
            session.close();
            return result;
        }
        return null;
    }

    public static Object findOne(String sqlName, Object params)
    {
        SqlSession session = JdbcUtils.getSession();

        if (session != null)
        {
            Object result = null;
            try
            {
                result = session.selectOne(sqlName, params);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            session.close();
            return result;
        }
        return null;
    }

    public static boolean update(String sqlName, Object params)
    {
        SqlSession session = JdbcUtils.getSession();
        if (session != null)
        {
            int result = session.update(sqlName, params);
            session.commit();
            session.close();
            return result == 0 ? false : true;

        }
        return false;
    }

    public static Object findOne(String sqlName)
    {
        SqlSession session = JdbcUtils.getSession();
        if (session != null)
        {
            Object result;
            result = session.selectOne(sqlName);
            session.close();
            return result;
        }
        return null;
    }

}
